import { IPoint } from "./gui";

export type IFn = (x: number) => number;
export type IFn2 = (t: number) => IPoint;

export type IMatrix1 = number[];
export type IMatrix2 = number[][];
export type IMatrix3 = number[][][];
export type IMatrix4 = number[][][][];

export function average (A: IMatrix1): number {
  let result = 0;
  A.forEach(a => {
    result += a;
  });
  return result / A.length;
}

export function gauss (x: number, o: number): number {
  return Math.exp(- x * x / 2 / o / o);
}

// 高斯求导
export function dgauss (x: number, o: number): number {
  return - x * gauss(x, o) / o / o;
}

export function distance2 (p1: IPoint, p2: IPoint): number {
  const p: IPoint = {
    x: p1.x - p2.x,
    y: p1.y - p2.y,
  };
  return Math.sqrt(p.x * p.x + p.y * p.y);
}